package model;

import java.util.SortedSet;
import java.util.TreeSet;

import users.Subscriber;

/**
 * Subscriber to a Qpid broker.
 */
public class Reader extends Subscriber {

    /** Name of the reader. */
    private String name;
    /**
     * Getter for the variable 'name'.
     * @return variable 'name'
     */
    public String getName() {
        return name;
    }
    /**
     * Setter for the variable 'name'.
     * @param name  new value of the variable 'name'
     */
    public void setName(final String name) {
        this.name = name;
    }

//    //messages not deleted
//    private Set<JMSTextMessage> pendingMsgs
//            = new LinkedHashSet<JMSTextMessage>();
    /** Relevant info of the messages not deleted. */
    private SortedSet<String> pendingMsgsInfo = new TreeSet<String>();

    /** Number of pending messages. */
    private int numMsgs = pendingMsgsInfo.size();

    /**
     * Getter for the variable 'numMsgs'.
     * @return variable 'numMsgs'
     */
    public int getNumMsgs() {
        return numMsgs;
    }
    /**
     * Setter for the variable 'numMsgs'.
     * @param numMsgs  new value of the variable 'numMsgs'
     */
    public void setNumMsgs(final int numMsgs) {
        this.numMsgs = numMsgs;
    }
//    public Set<JMSTextMessage> getPendingMsgs() {
//        return pendingMsgs;
//    }
//    public void setPendingMsgs(Set<JMSTextMessage> pendingMsgs) {
//        this.pendingMsgs = pendingMsgs;
//    }
    /**
     * Getter for the variable 'pendingMsgsInfo'.
     * @return variable 'pendingMsgsInfo'
     */
    public SortedSet<String> getPendingMsgsInfo() {
        return pendingMsgsInfo;
    }
    /**
     * Setter for the variable 'pendingMsgsInfo'.
     * @param pendingMsgsInfo  new value of the variable 'pendingMsgsInfo'
     */
    public void setPendingMsgsInfo(final SortedSet<String> pendingMsgsInfo) {
        this.pendingMsgsInfo = pendingMsgsInfo;
    }


    /**
     * Constructor for readers retrieved from the database.
     */
    public Reader() {
        //after retrieving a reader from the database, its scRecTopic,
        //scSubTopic, numSubs, numMsgs and listener MUST be set manually
    }

    /**
     * Constructor for new readers.
     *
     * @param name  name of the subscriber
     * @param username  username to use to connect to the broker
     * @param password  password to use to connect to the broker
     */
    public Reader(final String name, final String username,
                  final String password) {

        super(username, password);

        //set a ReaderMessageListener
        setListener(new ReaderMessageListener(this));

        this.name = name;

    }


    /**
     * Removes the message with ID msgId.
     *
     * @param msgId  ID of the message to remove
     */
    public void removeMsg(final String msgId) {

//        //remove from pendingMsgs
//        for (JMSTextMessage msg : getPendingMsgs()) {
//            try {
//                if (msg.getJMSMessageID().equals(msgId)) {
//                    getPendingMsgs().remove(msg);
//                    setNumMsgs(getPendingMsgs().size());
//                    break;
//                }
//            } catch (JMSException e) {
//                System.err.println("Error getting message ID");
//            }
//        }

        //remove from pendingMsgsInfo
        for (String msgInfo : getPendingMsgsInfo()) {
            if (msgInfo.contains(msgId)) {
                getPendingMsgsInfo().remove(msgInfo);
                setNumMsgs(getPendingMsgsInfo().size());
                break;
            }
        }

    }

}
